package com.foreknow.dao.impl;

import com.foreknow.bean.Guestbook;
import com.foreknow.dao.GuestbookDao;
import com.foreknow.db.DBManager;
import com.foreknow.db.JdbcTemplate;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GuestbookDaoImpl extends BaseDao implements GuestbookDao {
//添加
    @Override
    public int add(Guestbook guestbook) throws SQLException {
        String sql = "insert into guestbook(id,name,phone,email,title,content,createdtime) values(?,?,?,?,?,?,?)";
//        DBManager dbManager = DBManager.getInstance();
//        Connection conn = dbManager.getConnection();
//        JdbcTemplate jdbcTemplate = JdbcTemplate.getInstance();
//        jdbcTemplate.setConnection(conn);
        int a = jdbcTemplate.update(sql,guestbook.getId(),guestbook.getName(),guestbook.getPhone(),guestbook.getEmail(),guestbook.getTitle(),guestbook.getContent(),guestbook.getCreatedtime());
        return a;
    }
//修改
    @Override
    public int update(Guestbook guestbook) throws SQLException {
//        String sql = "select * from guestbook where id = ?";
//        GuestbookDaoImpl g = new GuestbookDaoImpl();
        String sql1 = "update guestbook set name=?,phone=?,email=?,title=?,content=?,createdtime=? where id = ? ";
//        EntityMapping entityMapping = mappingFactory1.getmapping(MappingFactory.GUESTBOOK_MAPPING);
//        Guestbook guestbook = g.query(id);
//        Guestbook guestbook = null;
//        if (list.size()>0){
//            guestbook = (Guestbook) list.get(0);
//        }
        int a = jdbcTemplate.update(sql1,guestbook.getName(),guestbook.getPhone(),guestbook.getEmail(),guestbook.getTitle(),guestbook.getContent(),guestbook.getCreatedtime(),guestbook.getId());
        return a;
    }
//删除
    @Override
    public int delete(Integer id) throws SQLException {
        String sql = "delete from guestbook where id =?";
        int a = jdbcTemplate.update(sql,id);
        return a;
    }
//查询
    @Override
    public Guestbook query(Integer id) throws SQLException {
        String sql = "select * from guestbook where id =?";
//        DBManager dbManager = DBManager.getInstance();
//        Connection conn = dbManager.getConnection();
//        JdbcTemplate jdbcTemplate = JdbcTemplate.getInstance();
//        jdbcTemplate.setConnection(conn);
        EntityMapping entityMapping = mappingFactory.getmapping(MappingFactory.GUESTBOOK_MAPPING);
        List<Object> list = jdbcTemplate.query(sql,entityMapping,id);
        if (list.size()>0){
            Guestbook guestbook = (Guestbook) list.get(0);
            return guestbook;
        }
        return null;
    }
//模糊查询
    @Override
    public List<Object> mohuquery(String name) throws SQLException {
        String sql = "select * from guestbook where name like ?";
        EntityMapping entityMapping = mappingFactory.getmapping(MappingFactory.GUESTBOOK_MAPPING);
        String name1 = "%"+name+"%";
        List<Object> list = jdbcTemplate.query(sql,entityMapping,name1);
            return list;
    }

    @Override
    public List<Object> allquery() {
        String sql = "select * from guestbook";
        EntityMapping entityMapping = mappingFactory.getmapping(MappingFactory.GUESTBOOK_MAPPING);
        List<Object> list = jdbcTemplate.query(sql,entityMapping);
        return list;
    }
}
